﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    public class GiniPurityCalculator:IPurityCalculator
    {
        #region IPurityCalculator Members
        /// <summary>
        /// 0=pure
        /// </summary>
        /// <param name="termFrequencies"></param>
        /// <returns></returns>
        public double CalculatePurityScore(Dictionary<int, int> termFrequencies)
        {
            double score = 0;
            int total = 0;
            foreach(int termID in termFrequencies.Keys)
            {
                total += termFrequencies[termID];
            }
            foreach(int termID in termFrequencies.Keys)
            {
                double relativeFreq = (double)termFrequencies[termID] / total;
                foreach(int termID2 in termFrequencies.Keys)
                {
                    if(termID==termID2)
                        continue;

                    double relativeFreq2 = (double)termFrequencies[termID2] / total;
                    score += relativeFreq*relativeFreq2;
                }
            }
            return 1 - score;
        }

        #endregion
    }
}
